let app = getApp()
let teacherInfo;
Page({
  data: {
    width: 375,
    heigh: 603,
    logo: '../../image/scan/tackPhoto.png',
    istakephone: 0,
    countDownNum: 0,
    timer: '',
  },

  onLoad: function(option) {
    this.setData({
      signInType: option.signInType ? option.signInType : ''
    })
  },

  onShow: function () {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          width: res.windowWidth,
          height: res.windowHeight,
        })
      }
    })
  },

  // 开始拍照
  takePhoto() {
    let _this  = this
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'low',
      success: (photo) => {
        wx.showLoading({
          title: '照片传输当中',
        })
        _this.setData({
          src: photo.tempImagePath,
          istakephone: 1
        })
        _this.getCanvasImg(photo.tempImagePath)
      },
      fail(res) {
        wx.showToast({
          title: '出错啦',
        })
      }
    })
  },
  error(e) {
    console.log(e.detail)
  },

  // 压缩
  getCanvasImg: function (tempFilePaths) {
    let _this = this;
    wx.getImageInfo({
      src: tempFilePaths,
      success: (res) => {
        let ctx = wx.createCanvasContext('photo_canvas');
        var towidth = 177; //按宽度344px的比例压缩
        var toheight = Math.trunc(177 * res.height / res.width);
        _this.setData({
          canvas_h: toheight
        })
        ctx.drawImage(tempFilePaths, 0, 0, res.width, res.height, 0, 0, towidth, toheight)
        ctx.draw(false, function () {
          wx.canvasToTempFilePath({
            canvasId: 'photo_canvas',
            fileType: "jpg",
            success: (res) => {
              wx.getImageInfo({
                src: res.tempFilePath,
                success: (res) => {
                  _this.uploadCanvasImg(res.path)
                }
              })
            },
            fail (res) {
              wx.showToast({
                title: '出错啦',
              })
            }
          }, this)
        })
      }
    })
  },

  // 获取照片url
  uploadCanvasImg: function (cavasImg) {
    let that = this;
    wx.getStorage({
      key: 'teacherInfo',
      success:(res) => {
        teacherInfo = res.data 
        wx.uploadFile({
          url: 'https://upload.52carlife.com/upload/WeChat/image',
          filePath: cavasImg,
          name: 'file',
          method: 'POST',
          formData: {
            organizationId: teacherInfo.organizationId
          },
          success(res) {
            let imgUrl = JSON.parse(res.data).data[0]
            that.uploadImgUrl(imgUrl)
          },
          fail: function (res) {
            wx.showToast({
              title: '上传失败',
              icon: 'success',
              duration: 2000,
            })
            setTimeout(function () {
              that.setData({
                countDownNum: 0,
                istakephone: 0
              })
            }, 1000)
          }
        })
      },
      fail( res ) {
        wx.showToast({
          title: '出错啦',
        })
      }
    })


  },

  // 上传照片
  uploadImgUrl: function (imgUrl) {
    let that = this
    let countDownNum = 0;
    wx.request({
      url: 'https://organization-teacher.52carlife.com/org/teacher/faceApi/addSearchTaskByUrl',
      method: 'POST',
      data: {
        "organizationId": teacherInfo.organizationId,
        "staffId": teacherInfo.staffId,
        "imgUrl": imgUrl,
        "signInType": that.data.signInType
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        wx.hideLoading()
        that.setData({
          timer: setInterval(function () {
            countDownNum++;
            that.setData({
              countDownNum: countDownNum
            })
            if (countDownNum == 60) {
              clearInterval(that.data.timer);
              setTimeout(function () {
                wx.showToast({
                  title: '上传成功',
                  icon: 'success',
                  duration: 800,
                  success: function () {
                    that.setData({
                      countDownNum: 0,
                      istakephone: 0
                    })
                  }
                })
              }, 1500)
            }
          }, 1)
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '上传失败',
          icon: 'success',
          duration: 2000,
        })
        setTimeout(function () {
          that.setData({
            countDownNum: 0,
            istakephone: 0
          })
        }, 1000)
      }
    })
  },

  // 跳转签到列表
  navlist: function () {
    wx, wx.navigateTo({
      url: '../pickList/pickList'
    })
  }
})